/*
 * JDateChooser.java
 *
 * Created on 7 de septiembre de 2008, 19:25
 */

package es.atareao.ferraplan.gui;

import es.atareao.alejandria.gui.ErrorDialog;
import es.atareao.queensboro.db.Conector;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ChangeListener;
import es.atareao.ferraplan.lib.Forma;
import es.atareao.ferraplan.lib.Grupo;
import es.atareao.queensboro.db.Condition;
import java.util.Vector;
import javax.swing.UIManager;

/**
 *
 * @author  Propietario
 */
public class JFormaChooser extends javax.swing.JPanel {
    //private final static long serialVersionUID=0L;
    /** Creates new form JDateChooser */
    public JFormaChooser() {
        super();
        initComponents();
        inicia();
        JFormaChooserDialog jfcd=new JFormaChooserDialog(this);
        jfcd.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            @Override
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    formPropertyChange(evt);
                }
        });
        this.setJfcd(jfcd);
        this.jPopupMenu1.add(this.getJfcd());
        //
        //
        //
    }
    private void formPropertyChange(java.beans.PropertyChangeEvent evt){
        if(evt.getPropertyName().equals("SelectedFormaInJFCD")){
            Object newValue=evt.getNewValue();
            if(newValue instanceof Forma){
                Forma forma=(Forma)newValue;
                this.jForma.setForma(forma);
                this._selectedForma = forma;
                //
                this.setFuera(!this.isFuera());
                this.jPopupMenu1.setVisible(false);
            }
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jForma = new es.atareao.ferraplan.gui.JForma();
        jButton = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(150, 150));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(150, 150));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.X_AXIS));
        add(jForma);

        jButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/ferraplan/img/forma.png"))); // NOI18N
        jButton.setFocusable(false);
        jButton.setMaximumSize(new java.awt.Dimension(40, 1000));
        jButton.setMinimumSize(new java.awt.Dimension(40, 25));
        jButton.setPreferredSize(new java.awt.Dimension(40, 25));
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActionPerformed(evt);
            }
        });
        add(jButton);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActionPerformed
        this.saca();
}//GEN-LAST:event_jButtonActionPerformed

    private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
        searchFor+=evt.getKeyChar();
        if(searchFor.length()==3){
            try {
                Forma forma = this.getSelectedForma().findFirst(new Condition("nombre", searchFor, Condition.IGUAL));
                if(forma!=null){
                    this.setSelectedForma(forma);
                }
            } catch (SQLException ex) {
                ErrorDialog.manejaError(ex);
            }
            searchFor="";
        }
    }//GEN-LAST:event_formKeyReleased
    private void saca(){
        int x = 0;
        int y = this.jForma.getY() + this.jForma.getHeight();
	this.jPopupMenu1.show(this.jForma, x, y);
        this.setFuera(!this.isFuera());
    }
    public void inicializa(Vector<Grupo> grupos,Vector<Forma> formas){
        this.getJfcd().inicializa(grupos,formas);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton;
    private es.atareao.ferraplan.gui.JForma jForma;
    private javax.swing.JPopupMenu jPopupMenu1;
    // End of variables declaration//GEN-END:variables
    private boolean _fuera=false;
    private JFormaChooserDialog _jfcd;
    private Forma _selectedForma;
    private ChangeListener _changeListener;
    private boolean _editable=true;
    private String searchFor;


    public  boolean isFuera() {
        return _fuera;
    }

    public void setFuera(boolean fuera) {
        this._fuera = fuera;
    }

    public JFormaChooserDialog getJfcd() {
        return _jfcd;
    }

    public void setJfcd(JFormaChooserDialog jfcd) {
        this._jfcd = jfcd;
    }

    public ChangeListener getChangeListener() {
        return _changeListener;
    }

    public void setChangeListener(ChangeListener changeListener) {
        this._changeListener = changeListener;
    }
    @Override
    public void setEnabled(boolean enabled){
        super.setEnabled(enabled);
        //this.jSVGWrapperImage.setEnabled(enabled);
        this.jButton.setEnabled(enabled);
    }
    @Override
    public boolean isEnabled(){
        return super.isEnabled();
    }
    public void setEditable(boolean editable){
        this._editable=editable;
        this.jButton.setEnabled(editable);
    }
    public boolean isEditable(){
        return this._editable;
    }
    public void inicia(){
        if(isEnabled()){
            if(isEditable()){
                this.jForma.setBackground(UIManager.getColor("TextField.background"));
                this.jButton.setEnabled(true);
            }else{
                this.jForma.setBackground(java.awt.SystemColor.info);
                this.jButton.setEnabled(false);
            }
        }else{
            this.jForma.setBackground(UIManager.getColor("TextField.disableBackground"));
            this.jButton.setEnabled(false);
        }        
    }

    /**
     * @return the _forma
     */
    public Forma getSelectedForma() {
        return _selectedForma;
    }

    /**
     * @param forma the _forma to set
     */
    public void setSelectedForma(Forma selectedForma) {
        this.getJfcd().setSelectedForma(selectedForma);
        this.jForma.setForma(selectedForma);
        this._selectedForma = selectedForma;
    }
    public void setSelectedId(String id){
        if(id==null){
            this.selectFirstElement();
        }else{
            this.getJfcd().setSelectedId(id);
            this.jForma.setForma(this.getJfcd().getSelectedForma());
            this._selectedForma=this.getJfcd().getSelectedForma();
        }
    }
    public void selectFirstElement(){
        this.getJfcd().selectFirstElement();
        this.jForma.setForma(this.getJfcd().getSelectedForma());
        this._selectedForma=this.getJfcd().getSelectedForma();
    }
}
